// 商店通用接口

const Good = require('../models/Goods/Good');
const ShopGood = require('../models/Shop/Shop_good');
const UserGood = require("../models/User/User_good");
const Equipment = require("../models/Equipments/Equipment");
const UserEquipment = require("../models/User/User_equipment");
const equip_tools = require('../controller/tools/equipment/equip');
const shop_tools = require('../controller/tools/shop/shop');


//购买商品接口（接口内判断）
exports.buygood = async (req, res) => {
    // 获取收到的数据
    
    // 获取用户id
    //console.log(req.user._id);
    let uid = req.user._id;
    // 获取装备id
    //console.log("商品id",req.body.sid);
    let sid = req.body.sid;   
    // 获取装备id
    //console.log(req.params.id);
    let eid = req.params.id;
    // 获取操作类型
    //console.log(req.body.option);
    let option = req.body.option;

    if (option == 'equipment') {
        if (await shop_tools.check_shop_good_full(sid)) {
            const User_Equipment_Fields = {};
            if (uid) User_Equipment_Fields.user = uid;
            if (eid) User_Equipment_Fields.equipment = eid;
            // 默认是金币，所以这里不操作，后面写个方法判断用什么价格
            if (req.body.price_type) User_Equipment_Fields.price_type = req.body.price_type;
            // 暂时用着默认写0
            if (req.body.price) User_Equipment_Fields.price = req.body.price;
            // 备用写个方法随机技能
            if (req.body.skill) User_Equipment_Fields.skill = req.body.skill;
            // 装备的数值需要写方法去随机出
            bs = await equip_tools.roll_equipment_battle_stat(eid);
            if (bs[0]) User_Equipment_Fields.strength = bs[0];
            if (bs[1]) User_Equipment_Fields.defense = bs[1];
            if (bs[2]) User_Equipment_Fields.magic = bs[2];
            if (bs[3]) User_Equipment_Fields.speed = bs[3];
            if (bs[4]) User_Equipment_Fields.accuracy = bs[4];
            // 默认未装备
            if (req.body.status) User_Equipment_Fields.status = req.body.status;
            if(await shop_tools.shop_good_reduce(sid)){
                new UserEquipment(User_Equipment_Fields)
                .save()
                .then(user_equipment => {
                    res.json({
                        code: 200,
                        msg: "购买成功",
                        data: null
                    });
                })
                .catch(err => {
                    res.json({
                        code: 0,
                        msg: "购买失败,原因：装备生成失败。",
                        data: null
                    });
                })
            }else{
                res.json({
                    code: 0,
                    msg: "购买失败，原因：库存问题",
                    data: null
                });
            }
        }
    } else if (option == 'good') {

    } else {
        return res.json({
            code: 0,
            msg: "错误的购买方法",
            data: null
        })
    }

};


//购买商品接口（装备）备用
// exports.buyequipment = async (req, res) => {
//     console.log(req.user._id);
//     console.log(req.params.id);
//     const newUserGood = {};
//     if (req.user._id) { newUserGood.user = req.user._id };
//     if (req.params.id) { newUserGood.goods = req.params.id };
//     new UserGood(newUserGood).save()
//         .then(good => res.json(good))
//         .catch(err => console.log(err));
// };